package com.filmbooks.presenter.movie;

import android.os.Bundle;
import android.widget.ImageView;

import com.filmbooks.activity.movie.MovieDetailActivity;
import com.filmbooks.cache.Cache;
import com.filmbooks.constant.BundleKeyConstant;
import com.filmbooks.contract.movie.MovieContract;
import com.filmbooks.detail.WebViewLoadActivity;
import com.filmbooks.fragment.movie.child.TopMoiveFragment;
import com.filmbooks.model.home.bean.movie.HotMovieBean;
import com.filmbooks.model.home.bean.movie.MovieMainModel;
import com.filmbooks.model.home.bean.movie.SubjectsBean;
import com.zyw.horrarndoo.sdk.base.BasePresenter;

import io.reactivex.functions.Consumer;

/**
 * Created by heshuxiang on 2018/3/2.
 * 描述：
 * 公司：盟将威
 * 作者：heshuxiang
 * 日期：2018/3/2.
 * 工程：FilmBooks
 * 包：com.filmbooks.presenter.movie
 */

public class MovieMainPresenter extends MovieContract.MoviePresenter {
    @Override
    public MovieContract.IMovieMainModel getModel() {
        return MovieMainModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void loadHotMovieList() {
        if (mIModel == null || mIView == null)
            return;
        mRxManager.register(mIModel.getHotMovieList().subscribe(new Consumer<HotMovieBean>() {
            @Override
            public void accept(HotMovieBean hotMovieBean) throws Exception {
                if (mIView == null) {
                    return;
                }
                mIView.updateContentList(hotMovieBean.getSubjects());
                Cache.saveHotMovieCache(hotMovieBean.getSubjects());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
                    if (mIView.isVisiable()) {
                        mIView.showToast("Network error");
                    }
                    if (Cache.getHotMovieCache().size() == 0) {
                        mIView.showNetworkError();
                    } else {
                        mIView.updateContentList(Cache.getHotMovieCache());
                    }
                }
            }
        }));

    }

    @Override
    public void onItemClick(int position, SubjectsBean item, ImageView imageView) {
        MovieDetailActivity.start(mIView.getBindActivity(), item, imageView);
    }


    @Override
    public void onHeaderClick() {
        mIView.startNewFragment(TopMoiveFragment.newInstance());
    }

    public static BasePresenter newInstance() {
        return new MovieMainPresenter();
    }
}
